/**
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.dingmouren.paletteimageview.factory;

import ohos.app.Context;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

import java.io.InputStream;

public class PixelMapFactory {

    /**
     * 通过图片资源ID获取位图对象
     *
     * @param context Context
     * @param resId   资源 id
     * @return 位图
     */
    public static PixelMap CreatePixelMap(Context context, int resId) {

        return CreatePixelMap(context, resId, 0, 0);
    }

    /**
     * 通过图片资源ID获取位图对象
     *
     * @param context Context
     * @param resId   资源 id
     * @param width   icon width
     * @param height  icon height
     * @return 位图
     */
    public static PixelMap CreatePixelMap(Context context, int resId, int width, int height) {
        InputStream drawableInputStream = null;
        try {
            drawableInputStream = context.getResourceManager().getResource(resId);
            ImageSource imageSource = ImageSource.create(drawableInputStream, null);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            if (width > 0 && height > 0) decodingOptions.desiredSize = new Size(width, height);
            decodingOptions.desiredRegion = new Rect(0, 0, 0, 0);
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            return imageSource.createPixelmap(decodingOptions);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (drawableInputStream != null) {
                    drawableInputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 根据位图创建位图
     *
     * @param pixelMap
     * @param width
     * @param height
     * @return
     */
    public static PixelMap CreatePixelMap(PixelMap pixelMap, int width, int height) {
        try {
            PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
            options.releaseSource = false;
            options.pixelFormat = PixelFormat.ARGB_8888;
            options.size = new Size(width, height);
            return PixelMap.create(pixelMap, options);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}